#!/usr/bin/env python
################################################################################
#            __  __            _   ____        _ _ ____        _              #
#           |  \/  | ___  __ _| |_| __ )  __ _| | | __ )  ___ | |_           #
#           | |\/| |/ _ \/ _` | __|  _ \ / _` | | |  _ \ / _ \| __|         #
#           | |  | |  __/ (_| | |_| |_) | (_| | | | |_) | (_) | |_          #
#           |_|  |_|\___|\__,_|\__|____/ \__,_|_|_|____/ \___/ \__|          #
#                                                                             #
#                                     by                                       #
# _______ .______       _______  _______   ______   ______    _______   _______ #
#|   ____||   _  \     |   ____||   ____| /      | /  __  \  |       \ |   ____| #
#|  |__   |  |_)  |    |  |__   |  |__   |  ,----'|  |  |  | |  .--.  ||  |__     #
#|   __|  |      /     |   __|  |   __|  |  |     |  |  |  | |  |  |  ||   __|     #
#|  |     |  |\  \----.|  |____ |  |____ |  `----.|  `--'  | |  '--'  ||  |____     #
#|__|     | _| `._____||_______||_______| \______| \______/  |_______/ |_______|   #
#                                                                                 #
#                          Channel Management Module                             #
#                             Modular FrameWork                                 #
#
# Coders
#
#	Lachlan Gilmour (chilli0) while mentored by Justin Giorgi (rebel_kid)
#
# Debuggers
#
#	Justin Giorgi (rebel_kid)
#	Lachlan Gilmour (chilli0)
#	David Liedle (spiderbyte)
#	(Polarina)
#	(prologic)
#
################################################################################
#                                                                              #
#                               GPL Licensed                                   #
#                                                                              #
################################################################################

# deops people
def deop(globalvars, msgvars):
	
	# Checks permissions
	if msgvars["features"][3]:
		if msgvars["accessperms"][9]:
		
			# DeOps
			msgvars["server"].mode(msgvars["channel"], '-o %s' % (msgvars["source"]))
		
		# Replies not authorized
		else:
			msgvars["server"].privmsg(msgvars["destination"], "Sorry but you're not allowed to do that.")
	else:
		msgvars["server"].privmsg(destination, "Sorry but that feature is disabled here.")
	
# Kicks on command
def kick(globalvars, msgvars):

	# Checks permissions
	if msgvars["features"][3]:
		if msgvars["accessperms"][9]:

			# Checks command length
			if len(msgvars["command"]) > 1:
		
				# Kicks
				if len(msgvars["command"]) > 2:
					msgvars["server"].kick(msgvars["channel"], msgvars["command"][1], ' '.join(msgvars["command"][2:]))
				else:
					msgvars["server"].kick(msgvars["channel"], msgvars["command"][1], "An op told me to kick you.")
			
			# Replies not enough info
			else: 
				msgvars["server"].privmsg(privmsg["destination"], "Wait a second, you didn't even tell me who to kick.")
		
		# Replies not authorized
		else:
			msgvars["server"].privmsg(msgvars["destination"], "Sorry but you're not allowed to do that.")
	else:
		msgvars["server"].privmsg(destination, "Sorry but that feature is disabled here.")
	
# Logs Messages		
def log(globalvars, msgvars):
	
	if msgvars["features"][3] > 1:
		globalvars["cursor"].execute("insert into logs (chanid, source, host, message) values (%s, %s, %s, %s)", (msgvars["chanid"], msgvars["source"], msgvars["host"], msgvars["message"]))

# ops people
def op(globalvars, msgvars):
	
	# Checks permissions
	if msgvars["features"][3]:
		if msgvars["accessperms"][9]:
		
			# DeOps
			msgvars["server"].mode(msgvars["channel"], '+o %s' % (msgvars["source"]))
		
		# Replies not authorized
		else:
			msgvars["server"].privmsg(msgvars["destination"], "Sorry but you're not allowed to do that.")
	else:
		msgvars["server"].privmsg(destination, "Sorry but that feature is disabled here.")
		
def autoload(globalvars):
	
	# Loads localalways
	localalways = {'log':log}
	for feature in localalways.items():
		if feature[0] not in globalvars["localalways"].keys():
			globalvars["localalways"][feature[0]] = feature[1]
			
	# Loads oncall
	oncall = {'deop':deop, 'kick':kick, 'op':op}
	for feature in oncall.items():
		if feature[0] not in globalvars["oncall"].keys():
			globalvars["oncall"][feature[0]] = feature[1]
	
def load(globalvars, msgvars):
	
	# Loads localalways
	localalways = {'log':log}
	for feature in localalways.items():
		if feature[0] not in globalvars["localalways"].keys():
			globalvars["localalways"][feature[0]] = feature[1]
		else:
			msgvars["server"].privmsg(msgvars["destination"], "There is already a local-always function named %s." % (feature[0]))
			
	# Loads oncall
	oncall = {'deop':deop, 'kick':kick, 'op':op}
	for feature in oncall.items():
		if feature[0] not in globalvars["oncall"].keys():
			globalvars["oncall"][feature[0]] = feature[1]
		else:
			msgvars["server"].privmsg(msgvars["destination"], "There is already an oncall function named %s." % (feature[0]))
	return True
			
def unload(globalvars, msgvars, reloadmodule = False):
	
	# Unloads localalways
	localalways = {'log':log}
	for feature in localalways.items():
		if feature[0] in globalvars["localalways"].keys():
			del globalvars["localalways"][feature[0]]
		else:
			msgvars["server"].privmsg(msgvars["destination"], "I have no clue what just happened.")
			
	# Unloads oncall
	oncall = {'deop':deop, 'kick':kick, 'op':op}
	for feature in oncall.items():
		if feature[0] in globalvars["oncall"].keys():
			del globalvars["oncall"][feature[0]]
	return True
